<template>
  <div>
    <el-card class="search-card">
      <basic-table-form
        ref="formData"
        :formColumns="FORM_ITEM_LIST"
        :dataSource="formData"
        :defaultExpandStatus="false"
        :handleSearchForm="handleQueryForm"
        @getFormData="getFormData"
        :colNumOfRow="4"
      >
      </basic-table-form>
    </el-card>
    <el-card class="table-card">
      <basic-table
        :showSelectCol="false"
        :loading="tableInfo.loading"
        :columns="tableInfo.columns"
        :dataSource="tableInfo.list"
        :pagination="pagination"
        :tableHeight="460"
        showActionCol
        :actionColWidth="100"
        :actionRender="renderTableBtnAction"
        @pageChange="handlePageChange"
        @pageSizeChange="handlePageSizeChange"
      >
        <div slot="content_context">
          <el-button
            v-if="showBtn('导出') && supplierExportConfig.inner"
            :loading="exportLoading"
            size="mini"
            type="primary"
            @click="handleExport"
            >导出</el-button
          >
          <el-button
            size="mini"
            type="primary"
            @click="handleAdd"
            >新增</el-button
          >
          <el-button
            size="mini"
            type="primary"
            @click="handleBatchAdd"
            >批量新增</el-button
          >
        </div>
      </basic-table>
    </el-card>
    <!-- 新增到货时间 -->
    <AddArrivalTime
      :visible.sync="addVisible"
      :selectData="selectCurrentRow"
      :logisticsCenterList="logisticsCenterList"
      @getTableData="getTableData"
    />
    <!-- 批量新增到货时间 -->
    <BatchAddArrivalTime
      :visible.sync="batchAddVisible"
      :logisticsCenterList="logisticsCenterList"
      @getTableData="getTableData"
    />
  </div>
</template>
<script>
import BasicTableForm from '@/components/supplier/BasicForm';
import BasicTable from '@/components/supplier/BasicTable';
import AddArrivalTime from './components/AddArrivalTime';
import BatchAddArrivalTime from './components/BatchAddArrivalTime';
import { FORM_ITEM_LIST, TABLE_COLUMNS } from './constants';
import BasicPage from '@/mixins/supplier/basicPage';
import { createExcelByExport, setAuthBtnId } from '@/utils/bizUtils';
import { getTableAction } from '@supplier/utils/btnsUtils';
import {
  queryArrivalTimeListService,
  exportArrivalTimeService,
  deleteArrivalTimeService
} from '@api/supplier/innerManagement/arrivalTimeManagement';
import { queryLogisticsCenterService } from '@api/supplier/components';

export default {
  mixins: [ BasicPage ], // 分页数据及方法 表单基础方法
  components: {
    BasicTableForm,
    BasicTable,
    AddArrivalTime,
    BatchAddArrivalTime,
  },
  data () {
    return {
      FORM_ITEM_LIST,
      addVisible: false,
      batchAddVisible: false,
      selectCurrentRow: {},
      formData: {
        supplierCode: '',
        supplierName: '',
        supplierType: null,
        supplierClassify: null,
        managementAreaId: '',
        logisticsCenter: '',
        createUser: '',
        createTime: [],
        arrivalTime: null,
      },
      logisticsCenterList: []
    };
  },
  created () {
    this.tableInfo.columns = TABLE_COLUMNS
    this.getTableData();
    this.getLogisticsCenterData();
  },
  methods: {
    renderTableBtnAction (h, { row }) {
      const actionList = [
        { label: '编辑', method: () => this.handleEdit(row), hasAuth: false },
        { label: '删除', method: () => this.handleDelete(row), hasAuth: false },
      ]
      return getTableAction({ h, row, actionList, showBtn: this.showBtn });
    },
    // 新增
    handleAdd () {
      this.addVisible = true;
      this.selectCurrentRow = null;
    },
    // 批量新增
    handleBatchAdd () {
      this.batchAddVisible = true;
    },
    // 编辑
    handleEdit (row) {
      this.addVisible = true;
      this.selectCurrentRow = row;
    },
    // 删除
    handleDelete (row) {
      this.$confirm('确定要删除此条数据吗？', '提示', {
        type: 'warning',
      }).then(() => {
        this.deleteArrivalTimeAction(row)
      }).catch(() => {});
    },
    // 删除接口调用
    async deleteArrivalTimeAction (row) {
      const res = await deleteArrivalTimeService(
        { id: row.id },
        state => { this.tableInfo.loading = state }
      )
      if (res) {
        this.$message.success('删除成功')
        this.getTableData()
      }
    },
    // 初始化查询条件
    initSearchParams () {
      const { arrivalTime, createTime = [] } = this.formData;
      const [arrivalTimeStart = '', arrivalTimeEnd = ''] = arrivalTime || [];
      const [createTimeStart = '', createTimeEnd = ''] = createTime;
      return _.omit({
        ...this.formData,
        arrivalTimeStart,
        arrivalTimeEnd,
        createTimeStart,
        createTimeEnd
      }, 'arrivalTime', 'createTime')
    },
    // 获取表格数据
    async getTableData () {
      let data = {
        ...this.initSearchParams(),
        ...this.pagination
      };
      setAuthBtnId()
      const res = await queryArrivalTimeListService(data, state => { this.tableInfo.loading = state })
      if (res) {
        const { records, total } = res.result;
        this.tableInfo.list = records;
        this.pagination.total = total;
      }
    },
    // 获取物流中心
    async getLogisticsCenterData () {
      const res = await queryLogisticsCenterService({ companyType: 4 })
      if (res) {
        const { data } = res.result;
        this.logisticsCenterList = data.map(item => ({
          label: item.companyName,
          value: item.companyId
        }));
      }
    },
    // 导出
    async handleExport () {
      const res = await exportArrivalTimeService(
        { ...this.initSearchParams() },
        state => { this.exportLoading = state }
      )
      if (res) {
        createExcelByExport({ excelTitle: '到货时间管理', res })
      }
    },
  }
};
</script>
